package com.zheng.study.company.service.impl;

import com.zheng.study.base.dao.BaseDao;
import com.zheng.study.base.service.impl.BaseServiceImpl;
import com.zheng.study.company.dao.PowerDao;
import com.zheng.study.company.entity.*;
import com.zheng.study.company.service.DutyService;
import com.zheng.study.company.service.PowerService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * newFile
 * ============================================================================
 * author : dzw
 * createDate:  2018/8/24 。
 * ============================================================================
 */
@Service
public class PowerServiceImpl extends BaseServiceImpl<Power, String> implements PowerService {

	@Autowired
	private PowerDao powerDao;
	@Autowired
	private DutyService dutyService;

	@Override
	public BaseDao<Power, String> getBaseDao() {
		return powerDao;
	}

	@Override
	public List<Power> getLevelPowers(Company company, Department department) {
		return powerDao.getLevelPowers(company, department);
	}

	@Override
	public List<Map<String, Object>> getLevelTreeMapList(Company company, Department department, Power exceptPower) {
		List<Power> powerList = getLevelPowers(company, department);
		if (exceptPower != null && powerList.contains(exceptPower)) {
			powerList.remove(exceptPower);
		}
		return recursivTreeMapList(powerList, null);
	}

	@Override
	public List<Power> getRootPowerList(Company company) {
		return powerDao.getRootPowerList(company);
	}

	@Override
	public List<Map<String, Object>> getLevelTreeMapList(Company company) {
		List<Power> rootPowerList = getRootPowerList(company);
		return recursionAllTreeMapList(rootPowerList, new ArrayList<Power>());
	}

	@Override
	public List<Map<String, Object>> getLevelTreeMapList(Company company, User user) {
		List<Power> rootPowerList = getRootPowerList(company);
		List<Power> userPowerList = getPowerListByUser(user);
		return recursionAllTreeMapList(rootPowerList, userPowerList);
	}

	@Override
	public boolean checkIsHasChildren(Power power) {
		return powerDao.checkIsHasChildren(power);
	}

	@Override
	public List<Power> getPowerListByUser(User user) {
		List<Duty> dutyList = dutyService.getUserDutyList(user);
		List<Power> powerList = new ArrayList<Power>();
		for(Duty duty : dutyList){
			powerList.add(duty.getPower());
		}
		return powerList;
	}

	/**
	 * 递归职权 返回 Tree结构
	 */
	private List<Map<String, Object>> recursivTreeMapList(List<Power> powerList, Power parent) {
		List<Map<String, Object>> powerTreeList = new ArrayList<Map<String, Object>>();
		if (powerList != null && powerList.size() > 0) {
			for (Power power : powerList) {
				Power parentPower = power.getParent();
				if ((parent == null && parentPower == null) || (parent != null && parentPower != null && parent.equals(parentPower))) {
					Map<String, Object> tempMap = new HashMap<String, Object>();
					tempMap.put("id", StringUtils.isEmpty(power.getId())?"":power.getId());
					tempMap.put("name", power.getDepartment()==null || power.getPost()==null ? "" : power.getDepartment().getName() + " - " + power.getPost().getName());
					tempMap.put("children", recursivTreeMapList(powerList, power));
					powerTreeList.add(tempMap);
				}
			}
		}
		return powerTreeList;
	}

	/**
	 * 递归职权（所有职权）（从高到低）
	 * @param powerList 顶级职权
	 * @param userPowerList 当前用户拥有的职权
	 */
	private List<Map<String, Object>> recursionAllTreeMapList(List<Power> powerList, List<Power> userPowerList){
		List<Map<String, Object>> powerTreeList = new ArrayList<Map<String, Object>>();
		if(powerList!=null && powerList.size()>0){
			for(Power power : powerList){
				List<Power> childrenPowerList = new ArrayList<Power>(power.getPowerSet());
				Map<String, Object> tempMap = new HashMap<String, Object>();
				tempMap.put("id", power.getId());
				tempMap.put("name", power.getDepartment()==null || power.getPost()==null ? "" : power.getDepartment().getName() + " - " + power.getPost().getName());
				tempMap.put("isParent", checkIsHasChildren(power));
				tempMap.put("checked", userPowerList.contains(power));
				tempMap.put("children", recursionAllTreeMapList(childrenPowerList, userPowerList));
				powerTreeList.add(tempMap);
			}
		}
		return powerTreeList;
	}

}
